package admin

import (
	"gitee.com/gopher2011/gin"
	"gitee.com/gopher2011/mall/app/dao"
	"gitee.com/gopher2011/mall/app/service"
	"net/http"
	"strconv"
)

// 限时购场次管理 /flash_session
var SmsFlashPromotionSession = &smsFlashPromotionSessionApi{}

type smsFlashPromotionSessionApi struct{}

// 添加场次 /create Post 参数: dao.SmsFlashPromotionSession
func (it *smsFlashPromotionSessionApi) Create(r *gin.Context) {
	// 将请求参数解析到 ptr上
	var ptr dao.SmsFlashPromotionSession
	err := r.Parse(&ptr)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
		r.Abort()
	}
	res, err := service.SmsFlashPromotionSession.Create(ptr)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "添加场次失败!"})
		r.Abort()
	} else {
		r.JSON(http.StatusOK, gin.H{"msg": "添加场次成功!", "data": res})
	}
}

// 修改场次 /update/:id Post 参数: id,dao.SmsFlashPromotionSession
func (it *smsFlashPromotionSessionApi) Update(r *gin.Context) {
	str := r.Param("id")
	id, _ := strconv.Atoi(str)
	// 将请求参数解析到 ptr上
	var ptr dao.SmsFlashPromotionSession
	err := r.Parse(&ptr)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
		r.Abort()
	}
	res, err := service.SmsFlashPromotionSession.Update(int64(id), ptr)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "修改场次失败!"})
		r.Abort()
	} else {
		r.JSON(http.StatusOK, gin.H{"msg": "修改场次成功!", "data": res})
	}
}

// 修改启用状态 /update/status/:id Post 参数: id,status
func (it *smsFlashPromotionSessionApi) UpdateStatus(r *gin.Context) {
	str := r.Param("id")
	id, _ := strconv.Atoi(str)
	form := r.PostForm("status")
	status, _ := strconv.Atoi(form)
	res, err := service.SmsFlashPromotionSession.UpdateStatus(int64(id), int32(status))
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "修改启用状态失败!"})
		r.Abort()
	} else {
		r.JSON(http.StatusOK, gin.H{"msg": "修改启用状态成功!", "data": res})
	}
}

// 删除场次 /delete/:id Get 参数: id
func (it *smsFlashPromotionSessionApi) Delete(r *gin.Context) {
	str := r.Param("id")
	id, _ := strconv.Atoi(str)
	res, err := service.SmsFlashPromotionSession.Delete(int64(id))
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "删除场次失败!"})
		r.Abort()
	} else {
		r.JSON(http.StatusOK, gin.H{"msg": "删除场次成功!", "data": res})
	}
}

// 获取场次详情 /:id Get 参数: id
func (it *smsFlashPromotionSessionApi) GetItem(r *gin.Context) {
	str := r.Param("id")
	id, _ := strconv.Atoi(str)
	res, err := service.SmsFlashPromotionSession.GetItem(int64(id))
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "获取场次详情失败!"})
		r.Abort()
	} else {
		r.JSON(http.StatusOK, gin.H{"msg": "获取场次详情成功!", "data": res})
	}
}

// 获取全部场次 /list Get 参数:无
func (it *smsFlashPromotionSessionApi) ListAll(r *gin.Context) {
	res, err := service.SmsFlashPromotionSession.ListAll()
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "获取全部场次失败!"})
		r.Abort()
	} else {
		r.JSON(http.StatusOK, gin.H{"msg": "获取全部场次成功!", "data": res})
	}
}

// 获取全部可选场次及其数量 /select_list Get 参数: flash_promotion_id 返回值: dao.SmsFlashPromotionSessionDetail
func (it *smsFlashPromotionSessionApi) List(r *gin.Context) {
	query := r.Query("flash_promotion_id")
	id, _ := strconv.Atoi(query)
	res, err := service.SmsFlashPromotionSession.List(int64(id))
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "获取全部可选场次及其数量失败!"})
		r.Abort()
	} else {
		r.JSON(http.StatusOK, gin.H{"msg": "获取全部可选场次及其数量成功!", "data": res})
	}
}
